package ac.biu.cs.textcat2011.coloreddoc;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Code to create HTML files based on given clusters
 * @author Asher Stern
 * @since Aug 18, 2011
 *
 */
public class AllDocsCreator
{
	public static final String ASSIGNMENT_PREFIX = "assignment";
	public static final String ASSIGNMENT_POSTFIX = "vector.txt";
	public static final String VECTOR_FILE_PREFIX = "blocks-suspicious-document";
	public static final String VECTOR_FILE_POSTFIX = "vector.txt";
	public static final String LOG_PREFIX = "logfile";
	public static final String LOG_POSTFIX = "vector.txt";
	public static final String HTML_POSTFIX = ".html";

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			int index=0;
			if (args.length<(index+1))throw new RuntimeException("args");
			String mixedDirName = args[index];
			++index;
			if (args.length<(index+1))throw new RuntimeException("args");
			String pureDirName = args[index];
			++index;
			if (args.length<(index+1))throw new RuntimeException("args");
			String vectorDirName = args[index];
			++index;
			if (args.length<(index+1))throw new RuntimeException("args");
			String assignmentDirName = args[index];
			++index;
			if (args.length<(index+1))throw new RuntimeException("args");
			String htmlDirName = args[index];
			++index;
			
			AllDocsCreator app = new AllDocsCreator(
					new File(mixedDirName),
					new File(pureDirName),
					new File(vectorDirName),
					new File(assignmentDirName),
					new File(htmlDirName)
					);
			
			app.go();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

	}
	
	
	
	public AllDocsCreator(File mixedDirectory, File pureDirectory,
			File vectorDirectory, File assignmentDirectory,
			File outputDirectoryOfHtmls)
	{
		super();
		this.mixedDirectory = mixedDirectory;
		this.pureDirectory = pureDirectory;
		this.vectorDirectory = vectorDirectory;
		this.assignmentDirectory = assignmentDirectory;
		this.outputDirectoryOfHtmls = outputDirectoryOfHtmls;
	}



	public void go() throws IOException
	{
		goForDirectory(mixedDirectory,"mixed");
		goForDirectory(pureDirectory,"pure");
		
	}
	
	private void goForDirectory(File logFilesDirectory,String pureOfMixed) throws IOException
	{
		File[] logFiles = logFilesDirectory.listFiles();
		for (File logFile : logFiles)
		{
			String id = idByFile(logFile);
			File vectorFile = new File(vectorDirectory,VECTOR_FILE_PREFIX+id+VECTOR_FILE_POSTFIX);
			File assignmentFile = new File(assignmentDirectory,ASSIGNMENT_PREFIX+id+ASSIGNMENT_POSTFIX);
			File htmlFile = new File(outputDirectoryOfHtmls,pureOfMixed+id+HTML_POSTFIX);
			DocCreator docCreator = new DocCreator(assignmentFile, vectorFile, htmlFile);
			docCreator.go();
		}
	}
	
	private String idByFile(File file)
	{
		Pattern pattern = Pattern.compile("[0-9]+");
		Matcher matcher = pattern.matcher(file.getName());
		boolean found = matcher.find();
		if (!found)throw new RuntimeException("Not found for file : "+file.getPath());
		String ret = matcher.group();
		return ret;
	}
	
	private File mixedDirectory;
	private File pureDirectory;
	private File vectorDirectory;
	private File assignmentDirectory;
	private File outputDirectoryOfHtmls;
}
